package com.lineage.server.world;

import com.lineage.server.model.Instance.L1DeInstance;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WorldDe {
  private static final Log _log = LogFactory.getLog(WorldDe.class);
  
  private static WorldDe _instance;
  
  private final ConcurrentHashMap<String, L1DeInstance> _isDe;
  
  private Collection<L1DeInstance> _allDe;
  
  public static WorldDe get() {
    if (_instance == null)
      _instance = new WorldDe(); 
    return _instance;
  }
  
  private WorldDe() {
    this._isDe = new ConcurrentHashMap<>();
  }
  
  public Collection<L1DeInstance> all() {
    try {
      Collection<L1DeInstance> vs = this._allDe;
      return (vs != null) ? vs : (this._allDe = Collections.unmodifiableCollection(this._isDe.values()));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return null;
    } 
  }
  
  public ConcurrentHashMap<String, L1DeInstance> map() {
    return this._isDe;
  }
  
  public L1DeInstance getDe(String key) {
    try {
      return this._isDe.get(key);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return null;
    } 
  }
  
  public void put(String key, L1DeInstance value) {
    try {
      this._isDe.put(key, value);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void remove(String key) {
    try {
      this._isDe.remove(key);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
